package com.woniu.controller;


import com.woniu.entity.Households;
import com.woniu.service.IHouseholdsService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
//户信息表
@RestController
public class HouseholdsController {

    @Autowired
    private IHouseholdsService householdsService;

    /**
     * 新增
     */
    @PostMapping("/households/save")
    private ResultVO save(@RequestBody Households holds) {
        boolean falg = false;
        try {
            falg = householdsService.save(holds);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (falg ? ResultVO.success() : ResultVO.failure());
    }

    /**
     删除
     */
    @PostMapping("/households/deleteById")
    private ResultVO deleteById(@RequestBody String hid){
        int rows=0;
        try {
            rows = householdsService.deleteById(hid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (rows>0 ? ResultVO.success() : ResultVO.failure());
    }

    /**
     修改
     */
    @PostMapping("/households/updateById")
    private ResultVO updateById(@RequestBody Households households){
        boolean falg=false;
        try {
            falg = householdsService.updateById(households);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (falg ? ResultVO.success() : ResultVO.failure());

    }

    /**
     查询全部
     */
    @PostMapping("/households/findAll")
    private ResultVO findAll(){
        List<Households> list=null;
        try {
             list = householdsService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);

    }

    /**
     查询一个
     */
    @PostMapping("/households/findById")
    private ResultVO findById(@RequestBody String hid){
        List<Households> list=null;
        try {
            list = householdsService.findById(hid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return ResultVO.success(list);

    }


}

